import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
/**
 ******************************************
 * @author HuangFeng  [2018年1月30日 下午2:08:42]
 * @version 1.0
 ******************************************
 */
public class Mianshi {
	
	static String url = "http://bm.scs.gov.cn/pp/gkweb/core-web-app/api/common/result/getbscj";
	static String[] zhunkaozhen = {"128251312620","128262021916","128262090529","128251311925","128251312224"};
	static String[] dizhima = {"510322","510300","510301","510302","510303","510304","510311","510321",//自贡
							   "510000","510100","510101","510104","510105","510106","510107","510108",
							   "510112","510113","510121","510122","510123","510124","510125","510129",
							   "510131","510132","510181","510182","510183","510184","510400","510401",
							   "510402","510403","510411","510421","510422","510500","510501","510502",
							   "510503","510504","510521","510522","510524","510525","510600","510601",
							   "510603","510623","510626","510681","510682","510683","510700","510701",
							   "510703","510704","510722","510723","510724","510725","510726","510727",
							   "510781","510800","510801","510802","510811","510812","510821","510822",
							   "510823","510824","510901","510902","510921","510922","510923","511001",
							   "511002","511011","511024","511025","511028","511101","511102","511111",
							   "500000","500101","500102","500103","500104","500105","500106","500107",
							   "500108","500109","500110","500111","500112","500113","500221","500222",
							   "500223","500224","500225","500226","500227","500228","500229","500230",
							   "500231","500232","500233","500234","500235","500236","500237","500238",
							   "500239","500240","500241","500242","500243","500381","500382","500383"
							   };
	static int[] d = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	static String[] sd = {"1","0","X","9","8","7","6","5","4","3","2"};
	static Map<String, CloseableHttpClient> httpclientMap = new HashMap<String, CloseableHttpClient>();
	
	public static void main(String[] args) throws ParseException {
		new Mianshi();
		for (String string : dizhima) {
			Thread1 t1 = new Thread1(string);
			t1.start();
		}
	}
	
	/**
	 ******************************************
	 * @author HuangFeng [2018年1月30日 下午3:25:18]
	 * @version 1.0
	 ******************************************
	 * @param zhunkao
	 * @param haoma
	 */
	public static boolean chaxun(String zhunkao, String haoma) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("SFcard", haoma);
		params.put("ZKcard", zhunkao);
		params.put("examkey", "20180011");
		String jsonString = doGet("mianshi", url, params);
		JsonObject json = new JsonObject();
		ObjectMapper om = new ObjectMapper();
		if(!jsonString.isEmpty()){
			try {
				json = om.readValue(jsonString, JsonObject.class);
			} catch (JsonParseException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if(json.getFlag().equals("1")) {
				Exam exam = json.getScore().getExam();
				System.out.println("==================================================================================");
				System.out.println(exam.getAac002()+exam.getAac003()+": "+exam.getAha03a()+
						exam.getAha03b()+" " +exam.getAha03c()+" " +exam.getAha03d());
				System.out.println("==================================================================================");
				return true;
			}
		}
		return false;
	}

	/**
	 ******************************************
	 * @author HuangFeng [2018年1月30日 下午3:19:34]
	 * @version 1.0
	 ******************************************
	 */
	public Mianshi() {
		build("mianshi", url );
	}
	
	/**
	 * 格式化日期对象
	 ******************************************
	 * @author 杜武全 [2016年6月24日 下午3:53:20]
	 * @version 1.0
	 ******************************************
	 * @param date 日期对象
	 * @param format 日期格式，建议使用DateUtil枚举值
	 * @return 指定格式的日期字符串
	 */
	public static String format(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}
	
	public static Date nextDay(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, 1);
		Date nextDay = calendar.getTime();
		return nextDay;
		
	}

	/**
	 * Get请求
	 ******************************************
	 * @author HuangFeng [2017年7月5日 下午1:10:47]
	 * @version 1.0
	 ******************************************
	 * @param name HttpClient对象名
	 * @param url 链接
	 * @param params 请求参数
	 * @return 结果
	 */
	public static String doGet(String name , String url, Map<String, Object> params) {
		StringBuilder sb = new StringBuilder("");
		if (null != params && !params.isEmpty()) {
			int i = 0;
			for (String key : params.keySet()) {
				if (i == 0) {
					sb.append("?");
				} else {
					sb.append("&");
				}
				sb.append(key).append("=").append(params.get(key));
				i++;
			}
		}
		CloseableHttpResponse response = null;
		HttpGet get = new HttpGet(url + sb.toString());
		String result = "";
		try {
			response = httpclientMap.get(name).execute(get);
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				if (null != entity) {
					result = EntityUtils.toString(entity, "UTF-8");
				}
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (null != response) {
				try {
					EntityUtils.consume(response.getEntity());
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}
		return result;
	}
	
	/**
	 * 构建httpclient
	 ******************************************
	 * @author HuangFeng [2017年7月5日 下午1:10:02]
	 * @version 1.0
	 ******************************************
	 * @param name 名称
	 * @param url 链接
	 * @return 对象
	 */
	private String build( String name , String url) {
		CloseableHttpClient httpClient = createSSLClientDefault();
		HttpGet get = new HttpGet(url);
		CloseableHttpResponse response = null;
		String result = "";
		try {
			response = httpClient.execute(get);
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity, "UTF-8");
			httpclientMap.put(name, httpClient);
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			if (null != response) {
				try {
					EntityUtils.consume(response.getEntity());
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}
		return result;
	}
	
	/**
	 * 创建HttpClient对象，信任所有签名
	 ******************************************
	 * @author HuangFeng [2017年7月5日 下午1:02:10]
	 * @version 1.0
	 ******************************************
	 * @return HttpClient对象
	 */
	private CloseableHttpClient createSSLClientDefault() {
		SSLContext sslContext;
		try {
			sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
				// 信任所有
				public boolean isTrusted(X509Certificate[] xcs, String string) {
					return true;
				}
			}).build();
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
			return HttpClients.custom().setSSLSocketFactory(sslsf).build();
		} catch (KeyStoreException ex) {
			ex.printStackTrace();
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		} catch (KeyManagementException ex) {
			ex.printStackTrace();
		}
		return HttpClients.createDefault();
	}
	
}
